import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class TestJDBCUpdate {

    public static void main(String[] args) throws SQLException {

        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/database1?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("root");

        Connection connection = dataSource.getConnection();

        Scanner in = new Scanner(System.in);
        System.out.println("请输入要修改的学生学号");
        int id = in.nextInt();
        System.out.println("请输入要更改的姓名");
        String name = in.next();

        String sql = "update student set name=? where id=?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,name);
        statement.setInt(2,id);

        statement.executeUpdate();

        statement.close();
        connection.close();

    }
}
